/**
 * 
 */
package slash.client.swing.controls;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Logger;

import slash.client.controllers.GameController;
import slash.game.Game;
import slash.game.Movement;

/**
 * @author gsh
 */
public class GamePanel extends JPanel {
	protected static Logger logger = Logger.getLogger(GamePanel.class.getName());
	private Game game;

	public Game getGame() {
		return game;
	}

	// TODO: this should really take a GameView probably
	public GamePanel(Game game) {
		super(new BorderLayout());
		this.game = game;
		GameController.get().setGamePanel(this);
		addComponents();
	}

	private void addComponents() {
		MapPanel map = new MapPanel(getGame());
		JScrollPane mapScrollPane = new JScrollPane(map);

		ControlPanel controls = new ControlPanel();
		JScrollPane controlScrollPane = new JScrollPane(controls);

		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				mapScrollPane, controlScrollPane);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(425);
		add(splitPane, BorderLayout.CENTER);
		
		KeyListener keyListener = new KeyListener() {
			public void keyPressed(KeyEvent keyEvent) {
				logger.info("Pressed: "+keyEvent.getKeyChar());
			}
			public void keyReleased(KeyEvent keyEvent) {
				logger.info("Released: "+keyEvent.getKeyChar());
			}
			public void keyTyped(KeyEvent keyEvent) {
				char key = keyEvent.getKeyChar();
				logger.info("Typed: "+key);
				switch (key) {
				case 'a':
					GameController.get().Move(Game.hero, Movement.Left);
					break;
				case 's':
					GameController.get().Move(Game.hero, Movement.Down);
					break;
				case 'w':
					GameController.get().Move(Game.hero, Movement.Up);
					break;
				case 'd':
					GameController.get().Move(Game.hero, Movement.Right);
					break;
				}
				GameController.get().getGamePanel().repaint();
			}
		};
		setFocusable(true);
		addKeyListener(keyListener);

		setPreferredSize(new Dimension(600, 600));
	}
}
